using System;
using System.Reflection;
using Vici.Mvc;

namespace Demo.ViciMvc
{
    [View("login")]
    public class login : BaseController
    {
        [BeforeAction]
        private void CreateJavascript()
        {
            View.RegisterJavascriptIncludeFromResource(false,Assembly.GetExecutingAssembly(),"Demo.ViciMvc.Controllers.login.checkLogin.js");
        }

        //
        // This method is mapped to "/login"
        // The text boxes "login" and "password" are mapped to method parameters
        //
        [Url("login")]
        public void Login(string login, string password)
        {
            if (IsPost() && login != null && password != null)
            {
                User user = DataService.FindUser(login, password);

                if (user != null)
                {
                    Session.User = user;

                    Redirect("~/main");
                }
            }
            
        }

        [Ajax("LoginController.CheckLogin")]
        public static bool CheckLogin(string login, string password)
        {
            string viewName = WebAppContext.AjaxContext.ViewName;

            return (DataService.FindUser(login, password) != null);
        }

        //
        // This action method is mapped to the URL /logoff
        //
        [Url("logoff")]
        public void LogOff()
        {
            Session.User = null;

            Redirect("~/index");
        }
    }
}
